﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microhard.Permission_MembershipImpl;
using Microhard.Protal.DTO;
using Microhard.Permission.Models;
using FluentValidation.Mvc;
using Microhard.Framework.MVCHelper.ViewEngine;
using Microhard.Framework.MVCHelper;
using Microhard.Core.DBUtility;
using Microhard.WarehouseInsp.Buz.DTO;
using Microhard.Core.Configuration;
using Microhard.Core;


namespace Microhard.WarehouseInsp.WebUI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            //使用自定义的ViewEngine,实现Mobile与PC请求选择不同的View
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new MicrohardViewEngine());
            //使用自定义controller factory
            ControllerBuilder.Current.SetControllerFactory(new UnityControllerFacotry());
            //注册area
            AreaRegistration.RegisterAllAreas();
            //注册filter与route
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            //使用FV作为默认的验证器
            ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider());
            DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;


            //permission对象映射
            PermissionMapBuilder.InitMap();
            //门户注册
            ProtalMapBuilder.InitMap();
            //监管仓库
            IDTOBuilder dto = new EntityModelMappingBuilder();
            dto.MappingBuild();
            //字典缓存初始化
            DictionaryCache dc = new DictionaryCache();
            dc.RefreshCache();
            PCDCache pcd = new PCDCache();
            pcd.RefreshCache();

            //检查系统是否满足启动需要
            //IConfigCheckList ifc = IoC.Resolve<IConfigCheckList>();
            //if (!ifc.SysInitCheck())
            //{
            //    throw new Exception("系统无法启动，请检查相关数据库连接以及配置项是否正确");
            //}
        }
    }
}